	
	function translate(inArray, callback)
	{	
		var pos = 0;		
		var length = 0;
		for(var i = 0; i < inArray.length; i++)
		{
			length += inArray[i].length;
			if(length > 1500 || i == inArray.length - 1)
			{
				//查找pos-> i的翻译
				var source = "";
				var endPos = i - 1;
				if(i == inArray.length - 1)
				{
					endPos = i;
				}
				source = inArray.slice(pos, endPos + 1).join("\n");
				
				var ajaxUrl = "http://openapi.baidu.com/public/2.0/bmt/translate?client_id=" + apiKey + "&q=" + encodeURIComponent(source) + "&from=auto&to=auto&callback=?";			
				
				$.ajax({url:ajaxUrl,
					type:"GET",
					dataType:"jsonp",
					context:{start:pos,end:endPos},
					success:function(response){
					callback(response.trans_result,this.start,this.end);
				}
					
				});
				pos = i;
				length = inArray[i].length;
			}			
		}
	}
	
	function loadLrc(songid, callback)//callback(musicUrl,LRCString)
	{
		var ajaxUrl = "http://ting.baidu.com/data/music/links?songIds=" + songid + "&callback=?";
		log(ajaxUrl);
		
		$.getJSON(ajaxUrl,
			function(response)
			{
				music = response.data.songList[0].songLink;					
				lrcUrl = "http://ting.baidu.com" + response.data.songList[0].lrcLink;
				var lrcUrl = jsonProxy + "/getdata.php?url=" + encodeURIComponent(lrcUrl) + "&callback=?";
				log(lrcUrl);
				$.getJSON(lrcUrl,
					function(response2)
					{											
						callback(music, response2.data);						
				});
		});		
	}
		
  	function  enjoy(songname, artist, songid , bStart)
	{	
		var type = 0;
		if(g_type == 1)
		{
			if(artist.charCodeAt(0) > 0xff)
			{
				type = 0;
			}
			else
			{
				type = 1;
			}
			g_type = type;
		}		
		
		log("enjoy:");
		status("Loading "+ songname + "……", 1);
		
		var urlComponent = "http://box.zhangmen.baidu.com/x?op=12&count=1&title=" + encodeURIComponent(songname) + "$$" + encodeURIComponent(artist) +  "$$$$";
		log(urlComponent);
		
		var ajaxUrl = jsonProxy + "/getxml.php?url=" + encodeURIComponent(urlComponent) +
			"&songid=" + songid + "&callback=?";
		log(ajaxUrl);
		
		$.getJSON(ajaxUrl,
			function(response)
			{									
				var music = "";
				var lrcid = 0;
				if(response.url instanceof Array)
				{
					music = response.url[0].encode;
					music = music.substring(0, music.lastIndexOf("/") + 1);
					music += response.url[0].decode;
					
					lrcid = response.url[0].lrcid;	
				}
				else
				{
					music = response.url.encode;
					music = music.substring(0, music.lastIndexOf("/") + 1);
					music += response.url.decode;		

					lrcid = response.url.lrcid;
				}
				log(music);
				if(lrcid == "0" && songid != null && songid != "0")
				{
					loadLrc(songid, function (musicUrl, lrcStr){							
						musicInit(music, lrcStr, type);
						if(bStart != null && bStart == true)
						{
							gotoMusic(500);							
						}						
					});
				}
				else if(lrcid == "0")
				{	
					musicInit(music, "[00:00]未搜索到相关歌词", 0);
					if(bStart != null && bStart == true)
					{
						gotoMusic(500);
					}					
				}
				else
				{
					lrc = "http://box.zhangmen.baidu.com/bdlrc/" + Math.floor(lrcid / 100) + "/" + lrcid + ".lrc";
					
					var lrcUrl = jsonProxy + "/getdata.php?url=" + encodeURIComponent(lrc) +
						"&callback=?";
					log(lrcUrl);
					$.getJSON(lrcUrl,
						function(response2)
						{							
							log("Lyrics retrieved len:" + response2.data.length );
							
							musicInit(music, response2.data, type);
							
							if(bStart != null && bStart == true)
							{
								gotoMusic(500);
							}
					});
				}
			});											
	}
	
	function fillList(songArray)
	{
		$("ul.songList").empty();
		log("song total:"+ songArray.length);
		for(var i = 0; i < songArray.length; i++)	
		{//待处理带括号的情况 to do
			$("ul.songList").append( "<li><span class='song'>" + (i + 1) + "." + songArray[i].song + "</span> <span class='enjoy ui-state-highlight'><a class='ui-icon ui-icon-pencil' href='#' onclick='g_type = 1;enjoy(\"" + songArray[i].song + "\",\"" + songArray[i].singer + "\"," + songArray[i].song_id + ",true);'>enjoy</a></span> <span class='enjoy ui-state-highlight'><a class='ui-icon ui-icon-play' href='#' onclick='g_type = 0;enjoy(\"" + songArray[i].song + "\",\"" + songArray[i].singer + "\"," + songArray[i].song_id + ",true);'>enjoy</a></span><span class='singer'>" + songArray[i].singer + "</span> <span style='clear:both;'>&nbsp;</span></li>");
		}		
	}	
	
	//以下变量作为timeUpdate的一个静态变量(类似与C语言，函数执行完仍存在值)
	var lastPos = -1;
	var bIsGoing = false;
	function timeUpdate(sec, totalSec)
	{		
		var pos = 1;
		var	topNow = 0;
		for(; pos < lrcJson.length; pos++)
		{
			if(lrcJson[pos].second > sec)
			{
				pos--;
				break;
			}
			else
			{
				topNow += $($('span.lyrics_line')[pos]).height();
			}
		}
		if( pos ==  lrcJson.length)
		{
			pos--;
		}
		
		if(totalSec - sec < 2 && g_score > 0)
		{			
			var resultStr = "";
			if(g_score > 0 && g_score < 26)
			{
				resultStr = "Sorry ,you just got a D.<br/> Got to be hard. <br/>Your score is " + g_score + " .";				
			}
			else if(g_score > 25 && g_score < 51)
			{
				resultStr = "You're soso .You just got a C.<br/>Got to be hard. <br/>Your score is " + g_score + " .";
			}
			else if(g_score > 50 && g_score < 76)
			{
				resultStr = "Nice .You got a B.<br/> More effort. <br/>Your score is " + g_score + " .";
			}
			else if(g_score > 75 && g_score < 90)
			{
				resultStr = "Perfect.You got an A!<br/>But you can do better.<br/>Your score is " + g_score + " .";
			}
			else if(g_score > 89)
			{
				resultStr = "Excellent.You got an S!<br/>You're extrodinary.<br/>Your score is " + g_score + " .";
			}
			else
			{
				return;
			}
			$("#lyrics").html(resultStr);
			$("#lyrics").css("top","100px");
			$("#lyrics").css("text-align","center");			
			$("#lyrics").css("font-size","2em");
			$("#lyrics").css("color","red");
			return;
		}
		
		if(lastPos == pos  || bIsGoing)
		{			
			return;
		}
				
		$($('span.lyrics_line')[lastPos]).removeClass('lyrics_line_on',100);
		$($('span.lyrics_line')[pos]).addClass('lyrics_line_on',500);

		if(lrcJson[pos].translation != null)
		{
			status(lrcJson[pos].translation);
		}
		else
		{
			status(lrcJson[pos].content);
		}
		topNow = lyricsMidY - topNow;
		bIsGoing = true;
		$("#lyrics").animate({top:topNow+"px"},"slow", function(){bIsGoing = false;});			
		lastPos = pos;		
	}
	
	var lrcJson;
	var lyricsMidY;
	function musicInit(music, lyrics, type)//1.歌曲地址 2.歌词
	{
		log("music:");
		log(music);
		g_score = 0;
		//html文档初始化
		$("#wrapper").empty();
		$("#wrapper").html('<audio preload="auto" controls id="myPlayer"><source src="' + music + '"/></audio><div class="box"><div id="lyrics" ></div></div>');

		//歌词相关初始化
		lrcJson = lrc2json(lyrics);
		log("Lrc to json,count:" + lrcJson.length);
		$("#lyrics").html(json2lyrics(lrcJson, type));
		if(type == 1)
		{
			$(".lrcWord").keydown(score);
		}
		
		/*下面是加载翻译的代码*/
		var inArray = Array();
		for(var i = 0 ; i < lrcJson.length; i++)
		{
			inArray[i] = HTMLDecode(lrcJson[i].content);
		}
		
		translate(inArray, function(outArray, start, end)
		{				
			for(var i = start; i < Math.min(end + 1, start + outArray.length); i++)
			{
				lrcJson[i]["translation"] = outArray[i - start].dst;				
			}			
		});
		/*翻译加载结束*/
		
		lyricsMidY = $(".box").height() / 2;

		$("#lyrics").draggable({axis:"y",drag:function(event,ui){
			/*var offsetTop = - (str2number($("#lyrics").css("top")) - lyricsMidY);
			var height = 0;
			var pos = 0
			for(; pos < lrcJson.length; pos++)
			{					
				height += $($('span.lyrics_line')[pos]).height();
				if(height > offsetTop)
				{
					break;
				}
			}*/
		}});
		
		//播放器初始化
		$( 'audio' ).audioPlayer(timeUpdate);			
		status("Music loading ……", 1);
	}

function score(event)
{//Bug exist,for every tab into a blank you fill right,you win.
	var totalScore = 0;
	var key = event.which;

	if (key == 13 || key == 9){		
		window.event.keyCode= 9;		
		if(trim($(this).val()).toLowerCase() == trim($(this).parent().children(".lrcInfo").val()).toLowerCase())
		{
			totalScore += 100 / $(".lrcWord").length;
			$(this).css("color","red");
			$(this).css("text-decoration","none");
			status("Perfect ! Your score is " + g_score + " .", 3);
		}   	
		else
		{
			$(this).css("color","blue");
			$(this).css("text-decoration","line-through");
			status("Sorry ,you are wrong! Your score is " + g_score + " .", 3);
		}
		g_score += Math.ceil(totalScore);		
	}
}	